package com.hzbank.juc.synWait;

import lombok.extern.slf4j.Slf4j;

/**
 * @description:
 * @author: libugu@cskaoyan.com
 * @date 2023/12/25 - 15:26
 */
@Slf4j
public class SyncWaitNotify {
    private int flag;
    private int loop;
    public SyncWaitNotify(int flag, int loop) {
        this.flag = flag;
        this.loop = loop;
    }

    public void print(int waitFlag, int nextFlag, String str)  {
        for (int i = 0; i < loop; i++) {
            synchronized (this){
                while (this.flag != waitFlag){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.debug(str);
                flag = nextFlag;
                this.notifyAll();
            }
        }

        
    }
    public static void main(String[] args) {
        SyncWaitNotify syncWaitNotify = new SyncWaitNotify(1, 5);
        new Thread(()->{
                syncWaitNotify.print(1,2,"a");
        },"t1").start();

        new Thread(()->{
                syncWaitNotify.print(2,3,"b");
        },"t2").start();

        new Thread(()->{
                syncWaitNotify.print(3,1,"c");
        },"t3").start();


    }
}
